/*  ----------------------------------------------------------------------------
 *  Arello Mobile
 *  Fast 3d Engine
 *  ----------------------------------------------------------------------------
 * Except where otherwise noted, this work is licensed under
 * http://creativecommons.org/licenses/by/3.0
 */

#include "MusicChanger.h"
#include "Manager.h"
#include <logs/Log.h>
#include <math/utl.h>
#include <system/assert.h>

namespace liba
{
namespace noise
{

MusicChanger::MusicChanger(double fadeout_time):
	channel( noise::Manager::get_hardware()->create_streaming_channel( noise::hard::Hardware::channel_type_2d ) ),
	global_volume( 1 ),
	current_volume( 1 ),
	fadeout_speed( 1.0 / fadeout_time )
{
//	channel->ChangeAttribute( noise::hard::Channel::loop_name, double(1) );
}

void MusicChanger::change_music(const filesystem::String & next_music)
{
//	next_music_name = next_music;
}

void MusicChanger::LifeCycle(time_float delta_time)
{

return;

	if( next_music_name == music_name )
	{
		// Hrissan: if we began to lower volume (to play second music) and then asked to play first music again...
		current_volume += delta_time * fadeout_speed;
		current_volume = math::clamp( current_volume, 0.0, 1.0 );
		channel->FindProperty<double>( noise::hard::Channel::volume_name )->Set( current_volume * global_volume );
	}
	else
	{
		current_volume -= delta_time * fadeout_speed;
		channel->FindProperty<double>( noise::hard::Channel::volume_name )->Set( current_volume * global_volume );

		if( current_volume < 0 || channel->is_finished() )
		{
			channel->stop();
			music_name = next_music_name;
			if( !music_name.empty() )
			{
				// Hrissan: order of the next 2 calls is important
				current_volume = 1;
				channel->FindProperty<double>( noise::hard::Channel::volume_name )->Set( current_volume * global_volume );
				liba::noise::DataSource* ds = noise::Manager::create_datasource( music_name );
				LIBA_ASSERT(ds && "File not found");
				channel->play(ds);
			}
		}
	}
}

void MusicChanger::change_volume(double volume)
{
return;
	global_volume = volume;
}

} // namespace noise
} // namesapce liba

